import axios, {
  AxiosRequestHeaders,
  AxiosResponse,
  InternalAxiosRequestConfig,
} from "axios";
import { useUserStore } from "../store/user";
import { layer } from "@layui/layui-vue";
import router from "../router";

type TAxiosOption = {
  timeout: number;
  baseURL: string;
};

const config: TAxiosOption = {
  timeout: 600000, //超时
  baseURL: "/127.0.0.1", //使用代理-在vite.config.ts设置
  // baseURL: "/sp.6-7-8-9.com", //使用代理-在vite.config.ts设置
  // baseURL: "https://sp.6-7-8-9.com/admin1",
};

class Http {
  service;
  constructor(config: TAxiosOption) {
    this.service = axios.create(config);
    /* 请求拦截 */
    this.service.interceptors.request.use(
      (config: InternalAxiosRequestConfig) => {
        const userInfoStore = useUserStore();
        if (userInfoStore.token) {
          (config.headers as AxiosRequestHeaders).token =
            userInfoStore.token as string;
        } else {
          if (router.currentRoute.value.path !== "/login") {
            router.push("/login");
          }
        }
        return config;
      },
      (error) => {
        return Promise.reject(error);
      }
    );

    /* 响应拦截 */
    this.service.interceptors.response.use(
      (response: AxiosResponse<any>) => {
        // console.log("响应拦截-response", response);
        if (response.status == 200) {
          switch (response.data.code) {
            // case 200:
              // console.log("响应拦截-200", response);
              // return response.data;
            case 909:
              console.log("响应拦截-909", response.data);
              layer.confirm("请重新登录", {
                closeBtn: false,
                title: false,
                icon: 2,
                yes: function () {
                  router.push("/login");
                  layer.closeAll();
                },
              });
              return {
                code: 0,
                msg: "请重新登录",
                url: "/src/api/http.ts",
                db: response,
              };
            default:
              return response.data;
          }
        } else {
          console.log("请求响应-意外[76]", response);
          return {
            code: 0,
            msg: "请求响应-意外",
            url: "/src/api/http.ts",
            db: response,
          };
        }
      },
      (error) => {
        // 响应拦截-错误
        console.log("响应拦截-错误", error);
        if (error.code == "ECONNABORTED") {
          console.log("响应拦截-错误-超时", error);
          layer.confirm("会话超时", {
            closeBtn: false,
            title: false,
            icon: 2,
            yes: function () {
              layer.closeAll();
            },
          });
        }
        layer.confirm(error.message, {
          title: "响应错误[100]",
          icon: 2,
          yes: function () {
            layer.closeAll();
          },
        });
        return {
          code: 0,
          msg: "响应拦截-错误",
          error_code: error.code,
          error_message: error.message,
          request_status: error.request.status,
          url: "/src/api/http.ts",
          db: error,
        };
        // return Promise.reject(error);
      }
    );
  }

  /* GET 方法 */
  get<T>(url: string, params?: object, _object = {}): Promise<any> {
    return this.service.get(url, { params, ..._object });
  }
  /* POST 方法 */
  post<T>(url: string, params?: object, _object = {}): Promise<any> {
    return this.service.post(url, params, _object);
  }
  /* PUT 方法 */
  put<T>(url: string, params?: object, _object = {}): Promise<any> {
    return this.service.put(url, params, _object);
  }
  /* DELETE 方法 */
  delete<T>(url: string, params?: any, _object = {}): Promise<any> {
    return this.service.delete(url, { params, ..._object });
  }
}

export default new Http(config);
